package com.huatai.datadevelop.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.huatai.datadevelop.entity.TableConfigLogEntity;
import com.huatai.datadevelop.mapper.TableConfigLogMapper;
import com.huatai.datadevelop.utils.PageUtils;
import com.huatai.datadevelop.utils.Query;
import com.huatai.datadevelop.vo.TableConfigLogVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service
public class TableConfigLogService extends ServiceImpl<TableConfigLogMapper, TableConfigLogEntity>{
    @Autowired
    private TableConfigService tableConfigService;

    /**
     * 分页获取数据
     *
     * @param params
     * @return
     */
    public PageUtils queryPage(Map<String, Object> params) {
        String name = (String) params.get("table_name");
        IPage<TableConfigLogEntity> page = this.page(
                new Query<TableConfigLogEntity>().getPage(params),
                new QueryWrapper<TableConfigLogEntity>().like(StringUtils.isNotBlank(name), "table_name", name)
                        .orderByDesc("log_id")//按id进行倒序查询
        );
        return new PageUtils(page);
    }

    /**
     * 异常数据重新入库
     * @param
     */
    public Boolean tableConfigLogAbnormalData(TableConfigLogVo vo){
		Boolean aBoolean = true;
        Integer logId = vo.getLogId();
        TableConfigLogEntity byId = this.getById(logId);
        String errorMessage = byId.getErrorMessage();
        if (errorMessage != null){
            String sqlMessage = byId.getSqlMessage();
			aBoolean = this.tableConfigService.getBaseMapper().insertDataSql(sqlMessage);
		}
		return aBoolean;
    }
}
